package com.help.controller;

import cn.hutool.core.util.ObjectUtil;
import com.help.common.api.CommonResult;
import com.help.common.exception.Asserts;
import com.help.common.util.JwtUtil;
import com.help.domain.HelpNote;
import com.help.domain.User;
import com.help.service.impl.HelpNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;


@Api(tags = "救援信息接口")
@RequestMapping("/help")
@RestController
public class HelpNoteController {
    @Autowired
    private HelpNoteService helpNoteService;

    @ApiOperation(value = "发布救援")
    @PostMapping("/release")
    public CommonResult release(
            @RequestHeader("token") @ApiParam("用户token") String token,
            @RequestParam("isUrgent") @ApiParam(value = "是否紧急") String isUrgent,
            @RequestParam("name") @ApiParam(value = "名字") String name,
            @RequestParam("locationAddress") @ApiParam(value = "地址") String locationAddress,
            @RequestParam("locationName") @ApiParam(value = "位置") String locationName,
            @RequestParam("locationLatitude") @ApiParam(value = "纬度") String locationLatitude,
            @RequestParam("locationLongitude") @ApiParam(value = "经度") String locationLongitude,
            @RequestParam("phone") @ApiParam(value = "手机号码") String phone,
            @RequestParam("date") @ApiParam(value = "日期") String date,
            @RequestParam("time") @ApiParam(value = "时间") String time,
            @RequestParam("detail") @ApiParam(value = "情况的描述") String detail) {
        Map<String, Object> userMap = JwtUtil.getUserMap(token);
        String openid = (String) userMap.get("openid");
        String nickName = (String) userMap.get("nickName");
        HelpNote helpNote = new HelpNote();
        helpNote.setOpenid(openid);
        helpNote.setIsUrgent(isUrgent);
        helpNote.setLocationAddress(locationAddress);
        helpNote.setLocationName(locationName);
        helpNote.setLocationLatitude(locationLatitude);
        helpNote.setLocationLongitude(locationLongitude);
        helpNote.setPhone(phone);
        helpNote.setDate(date);
        helpNote.setTime(time);
        helpNote.setDetail(detail);
//        helpNote.setUserName(nickName);
        helpNote.setUserName(name);
        boolean save = helpNoteService.save(helpNote);
        if (save) {
            return CommonResult.success(null, "发布成功！");
        }
        return CommonResult.failed("发布失败！");
    }
}
